#include <stdio.h>
#include <string.h>
#include <assert.h>

#include "string_help.h"

/* be used by core.c */
void string_pp_to_p_with_space(char *dst, int n, char *src[])
{
	assert(dst != NULL);
	assert(src != NULL);
	
	int dst_loc = 0;
	int i = 0;
	for ( ; i < n; i++) {
		sprintf(dst + dst_loc, "%s", src[i]);
		dst_loc += strlen(src[i]);
		dst[dst_loc++] = ' ';
	}
	dst[--dst_loc] = '\0';
}

/* be used by core.c */
void string_break(char *str, char dst[5][128])
{
	if (str == NULL || dst == NULL) {
		return;
	}

	char *pos_begin = str;
	char *pos_end = str;
	int len = 0;
	
	pos_end = strstr(pos_begin, ",");
	len = pos_end - pos_begin;
	strncpy(dst[0], pos_begin, len);
	dst[0][len] = '\0';
	pos_begin = pos_begin + len + 1;

	pos_end = strstr(pos_begin, ",");
	len = pos_end - pos_begin;
	strncpy(dst[1], pos_begin, len);
	dst[1][len] = '\0';
	pos_begin = pos_begin + len + 1;

	pos_end = strstr(pos_begin, ",");
	len = pos_end - pos_begin;
	strncpy(dst[2], pos_begin, len);
	dst[2][len] = '\0';
	pos_begin = pos_begin + len + 1;

	pos_end = strstr(pos_begin, ",");
	len = pos_end - pos_begin;
	strncpy(dst[3], pos_begin, len);
	dst[3][len] = '\0';
	pos_begin = pos_begin + len + 1;

	strcpy(dst[4], pos_begin);
}
